//路由表，如果有需要的话，在这里添加
//项目中可以使用匿名路由，也可以使用命名路由。
//项目中不使用路由注解，需要使用命名路由时，在这里手动添加

import 'dart:convert';
import 'dart:ui';

import 'package:flutter/cupertino.dart';

import '../page/desktop.dart';
import '../page/main_bottom_tab_page/friend.dart';
import '../page/main_bottom_tab_page/home.dart';
import '../page/main_bottom_tab_page/message.dart';
import '../page/main_bottom_tab_page/mine.dart';
import '../page/main_bottom_tab_page/personal.dart';
import '../page/main_bottom_tab_page/web_page.dart';
import '../utils/log_util.dart';
import 'customer_route_animatio.dart';
import 'not_found.dart';

///为防止路由调用时的硬编码，路由配置
var router = RouteMapSetting.instance;

///带动画的
class RouteMapSetting {
  ///单例构造器
  RouteMapSetting._();

  static final RouteMapSetting _instance = RouteMapSetting._();

  static RouteMapSetting get instance {
    return _instance;
  }

  //首页四个tab
  final RouteMap desktop = RouteMap._("desktop", const Desktop(), "主页面");
  final RouteMap home = RouteMap._("home", const Home(), "首页");
  final RouteMap message = RouteMap._("message", const Message(), "消息");
  final RouteMap friend = RouteMap._("friend", const Friend(), "朋友圈");
  final RouteMap personal = RouteMap._("personal", const Personal(), "主页");
  final RouteMap mine = RouteMap._("mine", const Mine(), "我的");
  final RouteMap webPage = RouteMap._("webPage", const WebPage(), "网页");
}

class RouteMap {
  String name;
  String routeName;
  Widget builder;

  RouteMap._(this.routeName, this.builder, this.name) {
    LogUtil.i("initRoute:$routeName");
    routeTable[routeName] = builder;
  }

  String get url {
    return "flutter:$routeName";
  }
}

/// 路由表
final Map<String, Widget> routeTable = {};
String? url = "";
String route1 = "";
dynamic arguments = "";
RouteSettings? routeSetting;

/// 动态生成路由
RouteFactory onGenerateRoute = (RouteSettings settings) {
  url = window.defaultRouteName.replaceAll("/", "").trim();
  var name = settings.name;
  if (url!.isNotEmpty) {
    Map<String, dynamic> data = json.decode(url!.trim());
    route1 = data["routeName"].toString();
    arguments = data["arguments"].toString();
    routeSetting = RouteSettings(name: route1, arguments: arguments);
    name = routeSetting!.name;
  }
  var getWidget = routeTable[name];
  Widget widget = getWidget ?? NotFoundPage();
  Route? route;
  if (name != "guidanceView" && name != "refreshToken" && name != "login" && name != "home") {
    route = CupertinoPageRoute(builder: (context) => widget, settings: url!.isNotEmpty ? routeSetting : settings);
  } else {
    var materialPageRoute = CustomAnimationRoute(widget, url!.isNotEmpty ? routeSetting : settings);
    route = materialPageRoute;
  }

  return route;
};
